package main

import "fmt"

// Golang中空接口和类型断言使用细节

type Address struct {
	Name  string
	Phone int
}

func main() {
	var userinfo = make(map[string]interface{})

	userinfo["username"] = "Mike"
	userinfo["age"] = 20
	userinfo["hobby"] = []string{"吃饭", "睡觉", "打保罗"}
	fmt.Println(userinfo["username"])
	fmt.Println(userinfo["age"])
	// fmt.Println(userinfo["hobby"][1]) // type interface {} does not support indexing 空接口切片不支持索引访问
	hobby2, _ := userinfo["hobby"].([]string)
	fmt.Println(hobby2[1]) // 通过断言获取的切片对象可以适用索引获取元素

	var addr1 = Address{
		Name:  "李四",
		Phone: 13420009999,
	}
	userinfo["address"] = addr1
	fmt.Println(userinfo["address"])
	// fmt.Println(userinfo["address"].Name) // type interface {} is interface with no methods 空接口结构体不支持"."访问属性
	address2, _ := userinfo["address"].(Address)
	fmt.Println(address2.Name) // 通过断言获取的结构体对象可以使用索引获取属性
}
